package com.itheima;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.Goods;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Arrays;

@SpringBootTest
public class testES {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 查询所有
     *  1. matchAll
     *  2. 将查询结果封装为Goods对象，装载到List中
     *  3. 分页。默认显示10条
     */
    @Test
    public void testMatchAll() throws IOException {
        //2 创建SearchRequest搜索请求对象
        SearchRequest searchRequest=new SearchRequest("goods");
        //4 创建查询条件构建器
        SearchSourceBuilder searchBuilder=new SearchSourceBuilder();
        //6 创建查询条件
        QueryBuilder queryBuilder= QueryBuilders.matchAllQuery();
        //5 添加查询条件,并进行分页
        searchBuilder.query(queryBuilder).from(0).size(100);
        //3 添加查询条件构建器
        searchRequest.source(searchBuilder);
        //1 开始执行查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        //7 获取查询结果
        //7.1 获取总记录数
        long total = response.getHits().getTotalHits().value;
        System.out.println("总记录数："+total);
        //7.2 获取数据
        SearchHit[] hits = response.getHits().getHits();
        //遍历打印
    /*Arrays.stream(hits).map(hit -> {
            //得到json数据
            String json = hit.getSourceAsString();
            //转换成Goods对象并返回
            return JSON.parseObject(json, Goods.class);
        }).forEach(goods -> System.out.println(goods));*/
        //简化
        Arrays.stream(hits)
                .map(hit -> JSON.parseObject(hit.getSourceAsString(), Goods.class))
                .forEach(goods -> System.out.println(goods));
    }


    @Test
    public void testBooleanQuery() throws IOException {
        //2 创建SearchRequest搜索请求对象
        SearchRequest searchRequest=new SearchRequest("goods");
        //4 创建查询条件构建器
        SearchSourceBuilder searchBuilder=new SearchSourceBuilder();
        //6 创建查询条件
        QueryBuilder queryBuilder= QueryBuilders.termQuery("brandName","华为");
        QueryBuilder filterQueryBuilder1 = QueryBuilders.matchQuery("title", "手机");
        QueryBuilder filterQueryBuilder2 = QueryBuilders.rangeQuery("price").gte(2000).lte(3000);

        BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
                .must(queryBuilder)
                .filter(filterQueryBuilder1)
                .filter(filterQueryBuilder2);
        //5 添加查询条件,并进行分页
        searchBuilder.query(queryBuilder1).from(0).size(100);
        //3 添加查询条件构建器
        searchRequest.source(searchBuilder);
        //1 开始执行查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        //7 获取查询结果
        //7.1 获取总记录数
        long total = response.getHits().getTotalHits().value;
        System.out.println("总记录数："+total);
        //7.2 获取数据
        SearchHit[] hits = response.getHits().getHits();
        //遍历打印
    /*Arrays.stream(hits).map(hit -> {
            //得到json数据
            String json = hit.getSourceAsString();
            //转换成Goods对象并返回
            return JSON.parseObject(json, Goods.class);
        }).forEach(goods -> System.out.println(goods));*/
        //简化
        Arrays.stream(hits)
                .map(hit -> JSON.parseObject(hit.getSourceAsString(), Goods.class))
                .forEach(goods -> System.out.println(goods));
    }
}
